<template>
  <div class="box">
    <el-button id="btnsize" icon="el-icon-refresh" type="danger" size="small" @click="containerRename">{{$t('ReButton_Rebuildcontainer')}}</el-button>
    <el-button id="btnsize" icon="el-icon-edit" style="background: #067FB5; color: #fff;" size="small" @click="containerEdit">{{$t('ReButton_Modifycontainer')}}</el-button>
  </div>
</template>

<script>
import post from '@/utils/post'
export default {
  props: ['item'],
  methods: {
    // 重命名容器
    containerRename () {
      if (!confirm('是否确认重建？重建后您未固化的数据可能会被删除！')) {
        return
      }
      let docker1 = `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${this.item.Id}/rename?name=1001010101010101010101"` // 修改名字
      post({ docker: docker1 }, res => {
        if (res >= 200 && res < 300) {
          this.containerRebuild()
        } else {
          this.$message({
            showClose: true,
            message: '网络错误，请稍候重试！',
            type: 'error'
          })
          this.loading.close()
        }
      }, this)
    },
    // 重建容器
    containerRebuild () {
      let data = {
        name: this.item.Name,
        WorkingDir: this.item.Config.WorkingDir,
        Volumes: this.item.Config.Volumes,
        User: this.item.Config.User,
        Tty: this.item.Config.Tty,
        OpenStdin: this.item.Config.OpenStdin,
        NetworkingConfig: this.item.NetworkSettings,
        MacAddress: this.item.NetworkSettings.MacAddress,
        Labels: this.item.Config.Labels,
        Image: this.item.Config.Image,
        Hostname: this.item.Config.Hostname,
        HostConfig: this.item.HostConfig,
        ExposedPorts: this.item.Config.ExposedPorts,
        Env: this.item.Config.Env,
        Entrypoint: this.item.Config.Entrypoint,
        Domainname: this.item.Config.Domainname,
        Cmd: this.item.Config.Cmd
      }
      const docker = `curl --unix-socket /var/run/docker.sock -s -H Content-Type:application/json -d '${JSON.stringify(data)}' http:/v1.40/containers/create?name=${this.item.Name}`
      post({ docker }, res => {
        if (res.message) {
          this.loading.close()
          return this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          })
        }
        if (res.Id) {
          this.containerRemove(res.Id)
        } else {
          this.$message({
            showClose: true,
            message: '网络错误，请稍候重试！',
            type: 'error'
          })
          this.loading.close()
        }
      }, this)
    },
    // 删除容器
    containerRemove (newid) {
      const docker2 = `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X DELETE "http:/v1.40/containers/${this.item.Id}?force=true&v=true"` // 删除一个容器
      post({ docker: docker2 }, res => {
        if (res >= 200 && res < 300) {
          this.containerStart(newid)
        } else {
          this.$message({
            showClose: true,
            message: '网络错误，请稍候重试！',
            type: 'error'
          })
          this.loading.close()
          this.$router.push('/Container')
        }
      }, this)
    },
    // 启动新创建的容器
    containerStart (id) {
      const docker = `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${id}/start"` // 启动容器
      post({ docker }, res => {
        if (res >= 200 && res < 300) {
          this.$message({
            showClose: true,
            message: '容器重建成功！',
            type: 'success'
          })
          this.$router.go(-1)
          this.loading.close()
        } else {
          this.$message({
            showClose: true,
            message: '容器启动失败！',
            type: 'error'
          })
          this.loading.close()
          this.$router.push('/Container')
        }
      }, this)
    },
    // 编辑容器
    containerEdit () {
      this.$router.push(`/Container/EditContainer/${this.item.Id}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.box{
  margin-left: 10px;
}
</style>
